/**
 * Created by shenhai on 15/9/5.
 */


var communityService = require('../../../services/mis/community/communityService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');

exports.communityList = function(req, res) {

    communityService.communityListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when communityList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityList...'));
        }else {
            /*  宝宝树66小区二次改造    mod   gct  20170915   start   */
            // 根据小区查询
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            //var data = results[1]; // 新 cascading.js
            // select.js mylist.forEach(function(item){报错，原因：只需要data是一个数组
            /*  宝宝树66小区二次改造    mod   gct  20170915   end   */
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};


exports.communityAdd = function(req, res) {

    //if (req.body.image1) {
    //
    //    toolUtil.uploadImageService(null, null, req, function (err, images) {
    //        if (err) {
    //            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
    //        } else {
    //            if (images.length > 0 && images[0]) {
    //                req.body.communityLogo = images[0];
    //            }
    //
    //            delete req.body.image1;
    //
    //            communityService.communityAddService(req, function(err, results) {
    //                if (err) {
    //                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityAdd...'));
    //                }else {
    //                    var data = {};
    //                    res.json(messageUtil.composeSucceededMessage(req, null, data));
    //                }
    //
    //            });
    //        }
    //    });
    //
    //}else {



        communityService.communityAddService(req, function(err, results) {
            if (err) {
                logger.error('Exception happen when communityAdd...' + err.message);
                res.json(messageUtil.composeFailedMessage(req, err.message));
            }else {
                var data = {};
                res.json(messageUtil.composeSucceededMessage(req, null, data));
            }

        });

    //}



};


exports.communityEdit = function(req, res) {

    //if (req.body.image1) {
    //
    //    toolUtil.uploadImageService(null, null, req, function (err, images) {
    //        if (err) {
    //            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
    //        } else {
    //            if (images.length > 0 && images[0]) {
    //                req.body.communityLogo = images[0];
    //            }
    //            delete req.body.image1;
    //
    //            communityService.communityEditService(req, function(err, results) {
    //
    //                if (err) {
    //                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityEdit...'));
    //                }else {
    //                    var data = {};
    //                    res.json(messageUtil.composeSucceededMessage(req, null, data));
    //                }
    //            });
    //        }
    //    });
    //
    //}else {

        communityService.communityEditService(req, function(err, results) {

            if (err) {
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityEdit...'));
            }else {
                var data = {};
                res.json(messageUtil.composeSucceededMessage(req, null, data));
            }
        });

    //}

};


exports.communitySwitch = function(req, res) {

    communityService.communitySwitchService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communitySwitch...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};


exports.communityImport = function(req, res) {

    communityService.communityImportService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityImport...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};




exports.communityModify = function(req, res) {

    communityService.communityImportTestService(req, function(err, results) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityModify...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

exports.test = function(req, res) {

    communityService.testService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when test...'));
        }else {
            var data = {count:results[1].count, location:results[1]};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};



exports.modify = function(req, res) {

    communityService.modifyService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when modify...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

exports.communityImportRecord = function(req, res) {

    communityService.communityImportRecordService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when communityImportRecord...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};


exports.supplementList = function(req, res) {

    communityService.supplementListService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when supplementList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};


exports.supplementStatusUpdate = function (req, res) {

    communityService.supplementStatusUpdateService(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};


exports.applyListList = function(req, res) {

    communityService.applyListListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when applyListList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when applyListList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["page"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};


exports.applyPass = function (req, res) {

    communityService.applyPassService(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.applyPassCheck = function (req, res) {

    communityService.applyPassCheckService(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when applyPassCheck...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when applyPassCheck...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    })
};


exports.applyReject = function (req, res) {

    communityService.applyRejectService(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

/////////////////////////////小区地图API////////////////////////////////////////////
//exports.communityLocationList = function(req,res)
//{
//    communityService.getCommunityLocation(req, function(err, results) {
//        if (err) {
//            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
//            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
//        } else {
//            res.json(messageUtil.composeSucceededMessage(req, null, results));
//        }
//    })
//}

exports.communityByLocationList = function(req,res)
{
    communityService.getCommunityByLocation(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    })
}

exports.communityCountList = function(req,res)
{
    communityService.getCommunityCount(req, function(err, results) {

        if (err) {
            logger.error('Exception happened when supplementStatusUpdate...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when supplementStatusUpdate...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    })
}

